//=============================================================================

#include "stdafx.h"
#include "inc/msgs.h"
#include "earthQuake.h"
#include "aEarthQuakeDlg.h"
#include "inc/aFilesWomb.h"

//=============================================================================

#define WORLD aWorld::awGetInstance()
#define WOMB aFilesWomb::afGetInstance()

//=============================================================================

aEarthQuakeDlg::aEarthQuakeDlg(CWnd* pParent)	: CDialog(IDD_EARTHQUAKE_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

//=============================================================================

void aEarthQuakeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, STATIC_RENDER, staticRender);
}

//=============================================================================

BOOL aEarthQuakeDlg::PreTranslateMessage(MSG* msg)
{
  if (msg->message == WM_KEYDOWN)
  {
    if (msg->wParam == VK_RETURN)
      return 0;

    if (msg->wParam == VK_ESCAPE)
      return 0;
  }

  return CDialog::PreTranslateMessage(msg);
}

//=============================================================================

BEGIN_MESSAGE_MAP(aEarthQuakeDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
  ON_WM_TIMER()

  //menu
  ON_COMMAND(MENU_MAIN_EXIT, onExit)
  ON_COMMAND(MENU_GROUND_LOAD_TGA, onLoadHeightmapTGA)

  //messages
  ON_MESSAGE(RENDER_LMD, &aEarthQuakeDlg::OnRenderLMD)
  ON_MESSAGE(RENDER_LMU, &aEarthQuakeDlg::OnRenderLMU)
  ON_MESSAGE(RENDER_MM, &aEarthQuakeDlg::OnRenderMM)
  ON_MESSAGE(RENDER_RMD, &aEarthQuakeDlg::OnRenderRMD)
  ON_MESSAGE(RENDER_RMU, &aEarthQuakeDlg::OnRenderRMU)
  ON_MESSAGE(RENDER_MMD, &aEarthQuakeDlg::OnRenderMMD)
  ON_MESSAGE(RENDER_MMU, &aEarthQuakeDlg::OnRenderMMU)
  ON_MESSAGE(RENDER_MWM, &aEarthQuakeDlg::OnRenderMWM)
END_MESSAGE_MAP()

//=============================================================================

BOOL aEarthQuakeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

  SetTimer(0, 1000/rendering.asGetFixedFPS(), 0);
  mainMenu.LoadMenu(IDR_MAINMENU);
  SetMenu(&mainMenu);
  popUp.LoadMenu(IDR_RMBPOPUP);
  if (!aeInitRender())
  {
    MessageBox(L"Can't init rendering context!");

    EndDialog(0);
  }

  WOMB.afWombIni();
  aeFillPopUpMenu();

  SetWindowText(WORLD.caption.asGetString());

  return TRUE;
}

//=============================================================================

bool aEarthQuakeDlg::aeInitRender()
{
  CRect rect;
  aVector pos(5.0, 5.0, 5.0);
  aVector view(0.0, 1.0, 0.0);
  aVector up(0.0, 1.0, 0.0);

  staticRender.arSetWnd(m_hWnd);
  staticRender.GetWindowRect(&rect);

  rendering.camera.acSetCamera(pos, view, up);

  return rendering.asInitOGL(staticRender.GetDC()->m_hDC, rect);
}

//=============================================================================

void aEarthQuakeDlg::aeDrawFrame()
{
  if (!rendering.asGetDC() || !rendering.asGetRC())
    return;

  if (wglGetCurrentContext() != rendering.asGetRC())
    wglMakeCurrent(rendering.asGetDC(), rendering.asGetRC());

  rendering.asGetFrameRate();
  rendering.asKeyPress();
  rendering.asBeginToPaint();
  aeDrawGrid(10, 10, 0.5f);
  
  WORLD.awDrawWorld();

  SwapBuffers(rendering.asGetDC());
}

//=============================================================================

void aEarthQuakeDlg::aeDrawGrid(unsigned int x, unsigned int y, float step)
{
  GLfloat minX = -float(x)/2, maxX = float(x)/2;
  GLfloat minZ = -float(y)/2, maxZ = float(y)/2;
  long i, steps;
  GLfloat add;

  glColor3ub(180, 180, 180);
  glEnable(GL_BLEND);
  glDisable(GL_TEXTURE_2D);
  glBegin(GL_LINES);
    for (add = GLfloat(minX), steps = long(x/step), i = 0; i <= steps; ++i, (add += step))
    {
      glVertex3f(add, 0, minZ);
      glVertex3f(add, 0, maxZ);
    }
    for (add = GLfloat(minZ), steps = long(y/step), i = 0; i <= steps; ++i, (add += step))
    {
      glVertex3f(minX, 0, add);
      glVertex3f(maxX, 0, add);
    }
  glEnd();
  glDisable(GL_BLEND);
  glEnable(GL_TEXTURE_2D);
}

//=============================================================================

void aEarthQuakeDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this);
		CRect rect;

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  	GetClientRect(&rect);
		dc.DrawIcon((rect.Width() - GetSystemMetrics(SM_CXICON) + 1) / 2, (rect.Height() - GetSystemMetrics(SM_CYICON) + 1) / 2, m_hIcon);
	}
	else
		CDialog::OnPaint();
}

//=============================================================================

void aEarthQuakeDlg::OnTimer(UINT_PTR timerId)
{
  if (timerId == 0)
    aeDrawFrame();
}

//=============================================================================

HCURSOR aEarthQuakeDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderLMD(WPARAM wp, LPARAM lp)
{
  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderLMU(WPARAM wp, LPARAM lp)
{
  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderMM(WPARAM wp, LPARAM lp)
{
  if (mode == CAMERAROTATE)
  {
    CRect rect;

    GetWindowRect(&rect);

    rendering.camera.acCheckMouse(rect.left + rendering.asGetX() + rendering.asGetW()/2, rect.top + rendering.asGetY() + rendering.asGetH()/2, rect.left + rendering.asGetX() + wp, rect.top + rendering.asGetY() + lp);
  }

  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderRMD(WPARAM wp, LPARAM lp)
{
  if (GetKeyState(VK_CONTROL) & 0x80)
  {
    POINT mp;

    GetCursorPos(&mp);

    popUp.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, mp.x, mp.y, this);

    return 0;
  }

  CRect winRect;

  ::GetClipCursor(&tmpClipRect);

  staticRender.GetWindowRect(&winRect);
  ClipCursor(&winRect);

  ShowCursor(0);

  mode = CAMERAROTATE;

  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderRMU(WPARAM wp, LPARAM lp)
{
  if (mode == CAMERAROTATE)
  {
    ClipCursor(&tmpClipRect);
    ShowCursor(1);

    mode = NORMAL;
  }

  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderMMD(WPARAM wp, LPARAM lp)
{
  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderMMU(WPARAM wp, LPARAM lp)
{
  return 0;
}

//=============================================================================

LRESULT aEarthQuakeDlg::OnRenderMWM(WPARAM wp, LPARAM lp)
{
  rendering.camera.acMoveCamera(rendering.asGetCameraSpeed()*(lp < 0 ? (-5):7));

  return 0;
}

//=============================================================================

void aEarthQuakeDlg::onLoadHeightmapTGA()
{
  CFileDialog dialog(TRUE, L"tga", L"*.tga", OFN_HIDEREADONLY, L"TARGA (*.tga)|*.tga||", 0, 0);

  if (dialog.DoModal() == IDOK)
  {
    switch(WORLD.awLoadHeightmapTGA(dialog.GetPathName().GetBuffer()))
    {
      case GRO_NULLED_PATH:
      {
        MessageBox(_T("Can't load heightmap because the path is NULL."), _T("Loading heightmap"), MB_ICONERROR);

        break;
      }
      case GRO_CANT_LOAD_HEIGHT_MAP:
      {
        MessageBox(_T("Can't load heightmap because file format is wrong."), _T("Loading heightmap"), MB_ICONERROR);

        break;
      }
      case GRO_CANT_COMPILE_NO_HEIGHTMAP:
      {
        MessageBox(_T("Can't compile heightmap 'cause the data is NULL."), _T("Compiling heightmap"), MB_ICONERROR);

        break;
      }
      default:
      {
        SetWindowText(WORLD.caption.asGetString());

        break;
      }
    }
  }
}

//=============================================================================

void aEarthQuakeDlg::onExit()
{
  EndDialog(0);
}

//=============================================================================

void aEarthQuakeDlg::aeFillPopUpMenu()
{
  aList<aString> charsList;

  WOMB.afGetCharacters(charsList);

  MENUITEMINFO menuItem;
//  popUp.InsertMenuItem(POPUP_ADD_CHARACTER, );
}

//=============================================================================
